package emd24.telstudyresources;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ProgressActivity extends Activity {

	private String progressFile = "progressFile";
	private ArrayList<String> submissionData = new ArrayList<String>();
	private String[] instanceOfSubmission = null;
	private final double PASS_MARK = .64;
	private float attemptPercentage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress);

		// this is where the loading from the internal file happens
		try{
			FileInputStream inputStream = openFileInput(progressFile);
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			String line = null;
			while ((line = reader.readLine()) != null) {
				submissionData.add(line);
			}
			inputStream.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		Button btnExit = (Button) this.findViewById(R.id.exit);

		final ListView v = (ListView) this.findViewById(R.id.progressList);
		v.setAdapter(new ProgressAdapter(this, submissionData));
		v.setClickable(true);
		v.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			  @Override
			  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				  viewSubmissionsDetails(position);
			  }
			});
		
		// Set button listener
		btnExit.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_progress, menu);
		return true;
	}
	
	private void viewSubmissionsDetails(int position){
		instanceOfSubmission = submissionData.get(position).split("\t");
		String[] breakDownSubmissionData = instanceOfSubmission[0].split(" ");
		attemptPercentage = (Integer.parseInt(instanceOfSubmission[1]) * 100.0f) / Integer.parseInt(instanceOfSubmission[2]);
		new AlertDialog.Builder(this).setMessage("Date: " 
		+ breakDownSubmissionData[0] + " "
				+ breakDownSubmissionData[1] + " "
				+ breakDownSubmissionData[2] + "\n" +
				"Time: " + breakDownSubmissionData[3] + " " + breakDownSubmissionData[4] + "\n" +
				"Score: " + instanceOfSubmission[1] + "/" + instanceOfSubmission[2] + 
				" (" + attemptPercentage + "%)").
		setNeutralButton("Done", new DialogInterface.OnClickListener() {
			// Set listener for when yes button is clicked, exit activity
			public void onClick(DialogInterface arg0, int arg1) {

			}
		}).show();
	}
	/**
	 * This subclass is an adapter for the ListView in the ProgressActivity. The
	 * class is private and cannot be accessed form other classes  
	 * 
	 * @author Matthew Hottinger
	 *
	 */
	private class ProgressAdapter extends BaseAdapter {

		private ArrayList<String> resultInstances;

		private ProgressAdapter(Context c, ArrayList<String> results){
			this.resultInstances = results;
			LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return this.resultInstances.size();
		}

		@Override
		public Object getItem(int position) {
			return this.resultInstances.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				LayoutInflater inflater = LayoutInflater.from(parent.getContext());
				convertView = inflater.inflate(R.layout.progress_row_view, parent, false);
			}
			instanceOfSubmission = submissionData.get(position).split("\t");
			TextView progress = (TextView) convertView.findViewById(R.id.time);
			progress.setText("Attempt: " + (position + 1));
			TextView submissionScore = (TextView) convertView.findViewById(R.id.submissionScore);
			attemptPercentage = (Integer.parseInt(instanceOfSubmission[1]) * 100.0f) / Integer.parseInt(instanceOfSubmission[2]);
			submissionScore.setText("Score: " + instanceOfSubmission[1] + "/" + instanceOfSubmission[2] + " (" + attemptPercentage + "%)");
			if ((Integer.parseInt(instanceOfSubmission[1]) * (1.0)) / Integer.parseInt(instanceOfSubmission[2]) >= 0 
					&& (Integer.parseInt(instanceOfSubmission[1]) * (1.0)) / Integer.parseInt(instanceOfSubmission[2]) < PASS_MARK) {
				//RED
				convertView.setBackgroundColor(0xFFDB7093);
			}
			else {
				//GREEN
				convertView.setBackgroundColor(0xFF90EE90);
			}
			return convertView;
		}
	}
}
